/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef TASKMANAGER_H
#define TASKMANAGER_H

#include "programengine.h"
enum E_TASK_ENGINE_STATE
{
    E_TASK_ENGINE_FREE,
    E_TASK_ENGINE_RUNNING
};

class ProgramEngineInfo
{
public:
    QString fileName;
    QThread *runningThread;
    ProgramEngine *ProgramEnginePointer;
    E_TASK_ENGINE_STATE state;

};

/**
 * @brief The TaskManager class 任务容器，保存所有新开的任务（call，run）
 */
class TaskManager
{
public:
    TaskManager(int robotIdIn);
    ~TaskManager();


public:
    int get_running_program_file_strings_task(int languageId, QString fileName,
                                                       QVector<QString> &programStrings);
    int clearAllMsg( );
    int getCurrentRobotRunFile(CurrentRunFile &runRobotFile);
    int setAllTaskCease();
    int setAllTaskRun();
    int setAllTaskStop();
    int addNewProgramEngine(ProgramEngineInfo programEngineInfo);
    int createNewProgramEngine(int fatherTaskIndex, int thisTaskIndex, RobotIo* ioControllerIn,
                               RegData *mainRegData, RsrInfo *tmpRsrInfo);
    int modifyProgramEningeFileName(int taskIndex, QString fileNameIn);
    int getCurrentTaskSize();
    int runTask(int moveTypeIn, EN_CONTROL_TYPE runModelIn,
                QString fileNameIn, int& taskIndexOut);
    int resetTask();

    int getProgramEngineDebugInfo(int taskIndex, ProgramEngineDebugInfo &infoOut);
    int getTaskInfo(TaskManagerInfo &taskManagerInfo);
    int setRunModel(EN_CONTROL_TYPE runModelIn);
    int setMoveType(int moveTypeIn);

private:
    int getRunningTaskNumber();

private:
    QVector <ProgramEngineInfo> programEngineList;
//    QVector <QThread *> taskThreadList;

    int robotId;
    pthread_mutex_t taskMutex;
    pthread_mutexattr_t taskMutexAttr;

};

#endif // TASKMANAGER_H
